package com.hjh.springboot_jpademo.Service;

import com.hjh.springboot_jpademo.Repository.ProviderRepository;
import com.hjh.springboot_jpademo.entity.Provider;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

@Service
public class ProviderService {
    @Resource
    private ProviderRepository providerRepository;

    @Transactional
    public Provider save(Provider provider){
        return providerRepository.save(provider);
    }

    @Transactional
    public void delete(int id){
        providerRepository.deleteById(id);
    }

    @Transactional
    public void update(Provider provider){
        provider.setProCode("ppi");
        provider.setProName("ppi");
    }

    public Iterable<Provider> getAll(){
        return providerRepository.findAll();
    }

    public Provider getById(Integer id){
        Optional<Provider> op = providerRepository.findById(id);
        return op.get();
    }

    public List<Provider> findByProCodeAndAndProName(String proCode, String proName){
        return providerRepository.findByProCodeAndAndProName(proCode,proName);
    }

    public List<Provider> findByProCodeLike(String proCode){
        return providerRepository.findByProCodeLike("%"+proCode+"%");
    }

    public Iterable<Provider> findAllSort(Sort sort){
        return providerRepository.findAll(sort);
    }

    public Page<Provider> findAllPage(Pageable pageable){
        return providerRepository.findAll(pageable);
    }
}
